package org.docshare.view;

import org.docshare.zhuishu.Config;

import android.R.integer;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class ReadViewNew extends View {

	private static final String TAG = "ReadViewNew";
	private TextUtilNew textUtil;
	private float down_x;
	private float down_y;

	@SuppressLint("NewApi") public ReadViewNew(Context context, AttributeSet attrs, int defStyleAttr,
			int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		init();
	}

	public ReadViewNew(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	public ReadViewNew(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		
	}

	public ReadViewNew(Context context) {
		super(context);
		init();
	}
	public void init(){
		textUtil = new TextUtilNew(new Rect(new Rect(Config.left_margin,Config.top_margin,this.getWidth()-Config.right_margin,this.getHeight()-Config.bottom_margin)));
		
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		textUtil.resize(new Rect(Config.left_margin,Config.top_margin,w-Config.right_margin,h-Config.bottom_margin));
	}
	
	
	@Override
	public void draw(Canvas canvas) {
		//super.draw(canvas);
		canvas.drawRect(new Rect(0,0,this.getWidth(),this.getHeight()), textUtil.backPaint);
		textUtil.draw(canvas);
		
		
		
	}
	public void setText(String text,String title){
		textUtil.setText(text.toString(), 0, Config.charPerLine,title);
		this.invalidate();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		Log.i(TAG,"onTouchEvent"+event.getAction());
		if(event.getAction() == MotionEvent.ACTION_DOWN)
		{
			Log.i("MyView","down event..." );
			down_x = event.getX();
			down_y = event.getY();
			return true;
		}else if(event.getAction() == MotionEvent.ACTION_UP)
		{
			Log.i("MyView","up event..." );
			float up_x = event.getX();
			float up_y = event.getY();
			int w = this.getWidth();
			int h = this.getHeight();
			float ds = (down_x - up_x)*(down_x - up_x) + (down_y - up_y)*(down_y-up_y);
			Log.i(TAG, String.format("point (%f,%f) , w=%d,h=%d\n",down_x,down_y,w,h));
			if(w < h)
			{
				if(down_x < w/2)
				{
					boolean b = textUtil.prevPage();
					msgIfFail(b, "已经是第一页");
				}else{
					boolean b = textUtil.nextPage();
					msgIfFail(b, "已经是最后一页");
				}
				
			}else{ //click�¼�
				if(down_y < h/2)
				{
					boolean b =textUtil.prevPage();
					msgIfFail(b, "已经是第一页");
				}else{
					boolean b =textUtil.nextPage();
					msgIfFail(b, "已经是最后一页");
				}
			}
			//this.invalidate();
			this.invalidate(new Rect(0,0,this.getWidth(),this.getHeight()));
			return true;
		}else{
			Log.i("MyView","downevent..." );
		}
		
		return true;
	}
	
	void msgIfFail(boolean b , String msg){
		if(b)return;
		Toast.makeText(this.getContext(), msg, Toast.LENGTH_SHORT).show();
		
	}

}
